<?php
require 'vendor/autoload.php';

class NewsManager {

	private $parser;
	private $sorter;
	private $textCleaner;
	private $dbConnection;
	public $feedsXml;
	private $rssSourceManager;

	function __construct($rssParser, $textCleaner, $sorter, $dbConnector,$rssSourceManager){
		$this->parser = $rssParser;
		$this->textCleaner = $textCleaner;
		$this->sorter = $sorter;
		$this->dbConnection=$dbConnector->connectToDB();
		$this->rssSourceManager = $rssSourceManager;
	}

	function setFeedsXml($feedsXml){
		$this->feedsXml = $feedsXml;
	}
	
	function getAllLatestNewsFromDB(){
		$newsArray = array();
		//DateLimit = last 24 hs News
		$dateLimit  = DateManager::getDengoDateLimit();
		$queryHandler = $this->dbConnection->prepare('SELECT n.Id, n.Title,n.Resume,n.Link,n.Image,n.PubDate,s.Name,s.Shortname FROM news AS n, sources AS s WHERE n.Source = s.Id AND n.PubDate >=  :dateLimit');
		$queryHandler->setFetchMode(PDO::FETCH_OBJ);
		$queryHandler->bindParam(':dateLimit', $dateLimit);
		$queryHandler->execute();
		while ($queryResult = $queryHandler->fetch()){
			$news = $this->transformDBNewsToPhpNews($queryResult);
			array_push($newsArray,$news);
		}
		return($newsArray);
	}
	
	private function transformDBNewsToPhpNews($dbNews){
		$news = new News();
		$news->setId($dbNews->Id);
		$news->setLink($dbNews->Link);
		$news->setPubDate(DateManager::convertToPhp($dbNews->PubDate));
		$news->setResume($dbNews->Resume);
		$news->setSource($dbNews->Name);
		$news->setShortName($dbNews->Shortname);
		$news->setTitle($dbNews->Title);
		$news->setImage($dbNews->Image);
		return ($news);
	}
	
	function getLatestNewsFromDBForSource($sourceShortName){
		$sourceID = $this->rssSourceManager->getSourceID($sourceShortName);
		$newsArray = array();
		$dateLimit  = DateManager::getYesterdayDateLimit();
		//$queryHandler = $this->dbConnection->prepare('SELECT n.Id, n.Title,n.Resume,n.Link,n.PubDate,s.Name,s.Shortname FROM news AS n, sources AS s WHERE s.Shortname = :shortName AND n.PubDate >=  :dateLimit AND n.Source = s.Id ORDER BY n.PubDate DESC');
		$queryHandler = $this->dbConnection->prepare('SELECT n.Id, n.Title,n.Resume,n.Link,n.Image,n.PubDate,s.Name,s.Shortname FROM news AS n, sources AS s WHERE n.Source = :sourceID AND n.PubDate >=  :dateLimit AND n.Source = s.Id ORDER BY n.PubDate DESC');
		$queryHandler->setFetchMode(PDO::FETCH_OBJ);
		$queryHandler->bindParam(':dateLimit', $dateLimit);
		//$queryHandler->bindParam(':shortName', $sourceShortName);
		$queryHandler->bindParam(':sourceID', $sourceID);
		$queryHandler->execute();
		while ($queryResult = $queryHandler->fetch()){
			$news = $this->transformDBNewsToPhpNews($queryResult);
			array_push($newsArray,$news);
		}
		return($newsArray);
	}
	
	function getNewsById($id) {
		$queryHandler = $this->dbConnection->prepare('SELECT n.Id, n.Title,n.Resume,n.Link,n.Image,n.PubDate,s.Name,s.Shortname FROM news AS n, sources AS s WHERE n.Id = :id AND n.Source = s.Id');
		$queryHandler->setFetchMode(PDO::FETCH_OBJ);
		$queryHandler->bindParam(':id', $id);
		$queryHandler->execute();
		$queryResult = $queryHandler->fetch();
		$news = $this->transformDBNewsToPhpNews($queryResult);
		return $news;
	}
	
	function  searchNewsByKeyWordFromAllSources($keywordsStringForDBSearch){
		$newsArray = array();
		$dateLimit  = DateManager::getDengoDateLimit();
		$queryHandler = $this->dbConnection->prepare('SELECT n.Id, n.Title,n.Resume,n.Link,n.Image,n.PubDate,s.Name,s.Shortname FROM news AS n, sources AS s WHERE match (`Title`,`Resume`) AGAINST ( :kw ) AND n.Source = s.Id AND  PubDate >=  :dateLimit');
		$queryHandler->setFetchMode(PDO::FETCH_OBJ);
		$queryHandler->bindParam(':kw', $keywordsStringForDBSearch);
		$queryHandler->bindParam(':dateLimit', $dateLimit);
		$queryHandler->execute();
		while ($queryResult = $queryHandler->fetch()){
			$news = $this->transformDBNewsToPhpNews($queryResult);
			array_push($newsArray,$news);
		}
		return($newsArray);
	}
	
	function  searchNewsByKeyWordFromEnabledSources($keywordsStringForDBSearch,$sourcesShortnameArray,$dateLimit){
		$newsArray = array();
		$sourceAmount = count($sourcesShortnameArray);
		if ($sourceAmount > 0){
			$sourcesString = $this->prepareSourcesForQuery($sourcesShortnameArray);
			$queryHandler = $this->dbConnection->prepare('SELECT n.Id, n.Title,n.Resume,n.Link,n.Image,n.PubDate,s.Name,s.Shortname FROM news AS n, sources AS s WHERE s.Shortname IN (' . $sourcesString . ') AND match (Title,Resume) AGAINST ( :kw ) AND  PubDate >= :dateLimit AND n.Source = s.Id ORDER BY n.PubDate DESC');
			$queryHandler->setFetchMode(PDO::FETCH_OBJ);
			$queryHandler->bindParam(':kw', $keywordsStringForDBSearch);
			$queryHandler->bindParam(':dateLimit', $dateLimit);
			//Binding All Sources
			foreach ($sourcesShortnameArray as $k => $sourceShortName){
				$queryHandler->bindValue( ':source_'.$k, $sourceShortName );
			}
			$queryHandler->execute();
			while ($queryResult = $queryHandler->fetch()){
				$news = $this->transformDBNewsToPhpNews($queryResult);
				array_push($newsArray,$news);
			}
		}
		return($newsArray);
	}
	
	private function prepareSourcesForQuery($sourcesShortnameArray){
		$sourcesString = '';
		$i=0;
		foreach ($sourcesShortnameArray as $sourceShortname){
			if ($sourcesString == ''){
				$sourcesString = ' :source_'.$i++;
			}
			else{
				$sourcesString = $sourcesString . ', :source_'.$i++;
			}
		}
		return ($sourcesString);
	}
}
